package com.admgt.resource.ui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.Timer;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
/**
 * 本机系统的文件目录树结构
 * @author James
 *
 */
public class LocalTree extends JTree{
	private static final long serialVersionUID = 1L;
	private Timer hoverTimer;
	protected TreePath lastPath;
	public LocalTree(){
		init();
	}
	private void init() {
		setModel(createTreeModel());
		
		addTreeExpansionListener(new TreeExpansionListener() {
			public void treeCollapsed(TreeExpansionEvent e) {
			}

			public void treeExpanded(TreeExpansionEvent e) {
				TreePath path = e.getPath();

				if (path != null) {
					FileNode node = (FileNode) path.getLastPathComponent();

					if (!node.isExplored()) {
						DefaultTreeModel model = (DefaultTreeModel) getModel();
						node.explore();
						model.nodeStructureChanged(node);
					}
				}
			}
		});
		setCellRenderer(new DefaultTreeCellRenderer() {
			private static final long serialVersionUID = 1L;
			public Component getTreeCellRendererComponent(JTree tree,
					Object value, boolean selected, boolean expanded,
					boolean leaf, int row, boolean hasFocus) {
				TreePath tp = tree.getPathForRow(row);
				if (tp != null) {
					FileNode node = (FileNode) tp.getLastPathComponent();
					File f = node.getFile();
					try {
						Icon icon = FileSystemView.getFileSystemView()
								.getSystemIcon(f);
						this.setIcon(icon);
						this.setLeafIcon(icon);
						this.setOpenIcon(icon);
						this.setClosedIcon(icon);
						this.setDisabledIcon(icon);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				return super.getTreeCellRendererComponent(tree, value,
						selected, expanded, leaf, row, hasFocus);
			}

		});
		super.setScrollsOnExpand(true);
		
		/**
		 * Set up a hover timer, so that a node will be automatically expanded
		 * or collapsed
		 * if the user lingers on it for more than a short time
		 */
		hoverTimer = new Timer(1000, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (lastPath == null) {
					return;
				}
				if (getRowForPath(lastPath) == 0)
					return; // Do nothing if we are hovering over the root node
				if (isExpanded(lastPath))
					collapsePath(lastPath);
				else
					expandPath(lastPath);
			}
		});
		hoverTimer.setRepeats(false); // Set timer to one-shot mode
		
	}
	private TreeModel createTreeModel() {
		File root = FileSystemView.getFileSystemView().getRoots()[0];
		FileNode rootNode = new FileNode(root);
		rootNode.explore();
		return new DefaultTreeModel(rootNode);
	}
	
}



